<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function LoginForm(){
    	layout(false);
    	
    	$this->action=U('LoginSave');
    	$this->act=U('RegisForm');
		$this->display();
	}
//*******************************************************************************************	
	public function LoginSave(){
		if(IS_POST){
			$username=addslashes(I('post.username'));
			$password=addslashes(I('post.password'));
			if(empty($username)){
				$this->error('请输入用户名','LoginForm');
			}
			if(empty($password)){
				$this->error('请输入密码','LoginForm');
			}
			$user=M('loginuser');
			$r=$user->where("username='{$username}'")->find();
			if(!empty($r)){
				if($r['password']!=$password){
					$this->error('密码不正确',U('Login/LoginForm'));
				}else{
					session('user_id',$r['id']);
					session('username',$r['username']);

					header("location:".U('Index/Index'));//登陆成功
				}
			}else{
				$this->error('该用户不存在,请先注册','RegisForm');
			}
		}else{
			$this->error('操作失败','loginform');
		}
	}
//*******************************************************************************************
	public function RegisForm(){
		layout(false);
		$this->login=U('LoginForm');
		$this->code=U('code');
		$this->action=U('RegisSave');
		$this->display();
	}
//*******************************************************************************************
	 public function code(){
		$config =    array(
			'fontSize'    =>    30,    // 验证码字体大小
			'length'      =>    3,     // 验证码位数
			'useNoise'    =>    false, // 关闭验证码杂点
		);
		$Verify =new \Think\Verify($config);
		$Verify->entry();
	 }
//*******************************************************************************************
	public function RegisSave(){
		$user=D('Loginuser');
		if(IS_POST){
			$Verify = new \Think\Verify();
            $y=I('post.code');
            $r=$Verify->check($y);
            if($r){
	            	if($user->create()){
					if($user->add()){
							$this->success('注册成功','LoginForm');
						}else{
							$this->error($user->getError());
						}
					}else{
						$this->error($user->getError());
					}
            }else{
            	$this->error('验证码不对','RegisForm');
            }
			
		}
		
	}
//*******************************************************************************************
	public function logout(){//退出登录，清空所有session
        session(null);
        header("location:".U('Index/Index'));//退出后进入商品展示首页
    }
}